package com.rzico.sbl.ui.statistic

import android.view.View
import android.widget.TextView
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCommonListBinding
import com.rzico.sbl.model.StockRecord
import com.rzico.sbl.other.LinearNormalDecoration
import com.rzico.sbl.ui.order.OrderDetailActivity
import com.rzico.sbl.viewmodel.StatisticStockViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.TimeHelper
import com.xinnuo.common.helper.doubleValue
import com.xinnuo.common.helper.dp2px
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common.helper.sp2px
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.slimadapter.SlimAdapter

class StockSupplyRecordActivity : BaseActivity(R.layout.activity_common_list) {

    private val mBinding by lazy { ActivityCommonListBinding.bind(getRootView()) }

    override fun getViewModel() = get<StatisticStockViewModel>()

    private var mSupplyId = ""
    private var mSupplyType = ""

    override fun initData() {
        initTitle(title = "修改明细")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding.listLayout.apply {
            emptyLayout.also {
                it.emptyHint.text = "暂无相关记录信息！"
            }

            recycleList.also { view ->
                view.loadLinear(
                    refresh = swipeRefresh,
                    onRefresh = { getData(0) },
                    onLoad = {
                        isLoadingMore
                            .conditionFalse {
                                isLoadingMore = true
                                getData(pageNum)
                            }
                    }
                )

                view.addItemDecoration(
                    object : LinearNormalDecoration() {
                        override fun getHeaderName(pos: Int): String =
                            "${(mList[pos] as StockRecord).letter}月"
                    }.apply {
                        setHeaderContentColor(getColorEx(R.color.background))
                        setTextColor(getColorEx(R.color.black))

                        setHeaderHeight(dp2px(40f))
                        setFooterHeight(dp2px(13f))
                        setTextSize(sp2px(15f))
                    }
                )

                mAdapter = SlimAdapter.creator()
                    .register<StockRecord>(R.layout.item_stock_supply_record) { jector, bean, _ ->
                        jector.apply {
                            text(R.id.item_record_memo, bean.memo.trimEnd('\n'))
                            text(R.id.item_record_time, bean.createDate.getTimeString())
                            text(R.id.item_record_barrel, bean.barrelName.orEmpty())
                            text(R.id.item_record_code, bean.sn.orEmpty())
                            text(
                                R.id.item_record_hint1,
                                conditionIf(
                                    mSupplyType == "1",
                                    "上期欠桶：",
                                    "上期欠款："
                                )
                            )
                            text(
                                R.id.item_record_hint2,
                                conditionIf(
                                    mSupplyType == "1",
                                    "本期欠桶：",
                                    "本期欠款："
                                )
                            )
                            text(
                                R.id.item_record_before,
                                conditionIf(
                                    mSupplyType == "1",
                                    bean.stock.formatInteger(),
                                    bean.stock.formatDecimal()
                                )
                            )
                            text(
                                R.id.item_record_after,
                                conditionIf(
                                    mSupplyType == "1",
                                    bean.balance.formatInteger(),
                                    bean.balance.formatDecimal()
                                )
                            )
                            text(R.id.item_record_name, bean.operator)

                            getView<View>(R.id.item_record_barrel_ll)
                                .isGone = bean.barrelName.isNullOrEmpty()
                            getView<View>(R.id.item_record_code_ll)
                                .isGone = bean.sn.isNullOrEmpty()

                            with<TextView>(R.id.item_record_count) {
                                val count = bean.quantity.doubleValue

                                it.setTextColor(
                                    getColorEx(
                                        conditionIf(
                                            count < 0,
                                            R.color.red,
                                            R.color.black
                                        )
                                    )
                                )

                                it.text = when {
                                    count < 0 ->
                                        conditionIf(
                                            mSupplyType == "1",
                                            bean.quantity.formatInteger(),
                                            bean.quantity.formatDecimal()
                                        )

                                    else ->
                                        conditionIf(
                                            mSupplyType == "1",
                                            "+${bean.quantity.formatInteger()}",
                                            "+${bean.quantity.formatDecimal()}"
                                        )
                                }
                            }

                            clicked {
                                bean.sn.notEmpty {
                                    when {
                                        !bean.orderId.isNullOrEmpty() ->
                                            startActivityEx<OrderDetailActivity>(
                                                "orderSn" to bean.sn
                                            )

                                        !bean.creditId.isNullOrEmpty() ->
                                            startActivityEx<StockStoreDetailActivity>(
                                                "storeId" to bean.creditId
                                            )
                                    }
                                }
                            }
                        }
                    }
                    .attachTo(view)
            }
        }
    }

    override fun initListener() {
        mSupplyId = intent.getExtra("supplyId")
        mSupplyType = intent.getExtra("type")
    }

    /**
     * 欠款、欠桶明细列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .supplyRecord(
                page = index,
                type = mSupplyType,
                supplyId = mSupplyId,
                onSuccess = {
                    mList.apply {
                        (index == 0)
                            .condition {
                                clear()
                                pageNum = 0
                            }
                        addItems(it)
                        it.notEmpty { pageNum++ }
                    }

                    mList.onEach {
                        it as StockRecord
                        it.letter = TimeHelper
                            .getInstance()
                            .getMonthOfDate(
                                it.createDate.getTimeString(),
                                "yyyy-MM-dd HH:mm:ss"
                            )
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

}